package com.lang.actions;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.lang.beans.Parcel;
import com.lang.beans.User;
import com.lang.service.ParcelService;
import com.opensymphony.xwork2.Action;

/**
 * 包裹
 * 
 * @author Lang
 * 
 */
public class ParcelAction implements Action {

	private ParcelService parcelservice;

	private String send_name; // 寄件人
	private String send_tel; // 联系电话
	private String send_addr; // 寄件地址
	private String send_postcode; // 寄件邮编
	private String weight; // 重量计价
	private String pay_type; // 付款方式
	private String size; // 包装方式(大小)
	private String receive_name; // 收件人
	private String receive_tel; // 联系电话
	private String receive_addr; // 收件地址
	private String receive_postcode; // 收件邮编

	private String parcelInfo; // json请求返回包裹全部信息
	private String result; // json请求返回的结果
	
	private List parcels;
	private String request; //请求类型 所有派件or所有收件

	public ParcelService getParcelservice() {
		return parcelservice;
	}

	public void setParcelservice(ParcelService parcelservice) {
		this.parcelservice = parcelservice;
	}

	public String getSend_name() {
		return send_name;
	}

	public void setSend_name(String send_name) {
		this.send_name = send_name;
	}

	public String getSend_tel() {
		return send_tel;
	}

	public void setSend_tel(String send_tel) {
		this.send_tel = send_tel;
	}

	public String getSend_addr() {
		return send_addr;
	}

	public void setSend_addr(String send_addr) {
		this.send_addr = send_addr;
	}

	public String getSend_postcode() {
		return send_postcode;
	}

	public void setSend_postcode(String send_postcode) {
		this.send_postcode = send_postcode;
	}

	public String getWeight() {
		return weight;
	}

	public void setWeight(String weight) {
		this.weight = weight;
	}

	public String getPay_type() {
		return pay_type;
	}

	public void setPay_type(String pay_type) {
		this.pay_type = pay_type;
	}

	public String getSize() {
		return size;
	}

	public void setSize(String size) {
		this.size = size;
	}

	public String getReceive_name() {
		return receive_name;
	}

	public void setReceive_name(String receive_name) {
		this.receive_name = receive_name;
	}

	public String getReceive_tel() {
		return receive_tel;
	}

	public void setReceive_tel(String receive_tel) {
		this.receive_tel = receive_tel;
	}

	public String getReceive_addr() {
		return receive_addr;
	}

	public void setReceive_addr(String receive_addr) {
		this.receive_addr = receive_addr;
	}

	public String getReceive_postcode() {
		return receive_postcode;
	}

	public void setReceive_postcode(String receive_postcode) {
		this.receive_postcode = receive_postcode;
	}

	public void setParcelInfo(String parcelInfo) {
		this.parcelInfo = parcelInfo;
	}
	
	public String getParcelInfo() {
		return parcelInfo;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public List getParcels() {
		return parcels;
	}

	public void setParcels(List parcels) {
		this.parcels = parcels;
	}

	public String getRequest() {
		return request;
	}

	public void setRequest(String request) {
		this.request = request;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 添加包裹类
	 * 
	 * @return
	 */
	public String useraddparcel() {
		/*
		 * System.out.println("send_name = "+ this.getSend_name());
		 * System.out.println("send_tel = "+ this.getSend_tel());
		 * System.out.println("send_address = "+ this.getSend_addr());
		 * System.out.println("send_postcode = "+ this.getSend_postcode());
		 * System.out.println("weight = "+ this.getWeight());
		 * System.out.println("pay_type = "+ this.getPay_type());
		 * System.out.println("size = "+this.getSize());
		 * System.out.println("receive_name = "+ this.getReceive_name());
		 * System.out.println("receive_tel = "+ this.getReceive_tel());
		 * System.out.println("receive_addr = "+ this.getReceive_addr());
		 * System.out.println("receive_postcode = "+
		 * this.getReceive_postcode());
		 */
		// 获取当前用户
		User user = (User) ServletActionContext.getRequest().getSession()
				.getAttribute("user");

		Parcel parcel = new Parcel();
		parcel.setUser(user);
		parcel.setSendName(this.getSend_name());
		parcel.setSendTel(this.getSend_tel());
		parcel.setSendAddr(this.getSend_addr());
		parcel.setRecName(this.getReceive_name());
		parcel.setRecTel(this.getReceive_tel());
		parcel.setRecAddr(this.getReceive_addr());
		parcel.setSize(Integer.parseInt(this.getSize()));
		parcel.setType(new Integer(1));// 寄件
		parcel.setWeight(Float.parseFloat(this.getWeight()));
		parcel.setTransType(new Integer(1));// 运送方式,预设1空运
		parcel.setStatus(new Integer(0));// 状态,默认未处理
		parcel.setPayType(Integer.parseInt(this.getPay_type()));
		// 添加到数据库
		parcelservice.addPackage(parcel);
		return "success";
	}

	/**
	 * 获取包裹的详细信息
	 * 
	 * @return parcel
	 * @author Lang
	 */
	public String getParcelInfoById() {
		String parcelid = ServletActionContext.getRequest().getParameter(
				"parceid");   //获取ajax的data
		System.out.println("parcel.id=" + parcelid);
		parcelInfo = parcelservice.getParcelInfoById(parcelid); //获取包裹的json串
		System.out.println("ParcelInfo=" + parcelInfo);
		return "result"; 
	}
	/**
	 * 获取全部派件
	 * @return
	 */
	public String getAllSendParcels(){
	    this.request="所有派件";
		parcels=parcelservice.getAllSendParcels();
		return "success";
	}
	
	/**
	 * 获取全部收件
	 * @return
	 */
	public String getAllReceiveParcels(){
		this.request="所有收件";
		parcels=parcelservice.getAllReceiveParcels();
		return "success";
	}

}
